import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from '../views/home/main.vue'
import Home from '../views/home/main-content.vue'
import Apply from '../views/apply/apply-main.vue'
import ApplyCheckRedirect from '../views/apply/apply-check-redirect.vue'
import ApplyCheck from '../views/apply/apply-check.vue'
import ApplyPersonal from '../views/apply/apply-personal.vue'
import ApplyCompany from '../views/apply/apply-company.vue'
import ApplyAssets from '../views/apply/apply-assets.vue'
import ApplyScheme from '../views/apply/apply-scheme.vue'
import ApplyGuarantee from '../views/apply/apply-guarantee.vue'
import ApplyAttachment from '../views/apply/apply-attachment.vue'
import Query from '../views/query/query-main.vue'
import Create from '../views/create/create-main.vue'
import Report from '../views/report/report-main.vue'
import Login from '../views/login/login.vue'
import P404 from '../views/common/404.vue'

import store from '../store/index'

Vue.use(VueRouter)

/**
 * 路由配置
 */
const router = new VueRouter({
  routes: [
    {
      path: '/',
      name: 'main',
      component: Main,
      redirect: { path: 'home' },
      children: [
        { path: 'home', component: Home, meta: { requireLogin: true } },
        {
          path: 'apply',
          component: Apply,
          redirect: { path: '/apply/check' },
          children: [
            {
              path: '/apply/check',
              component: ApplyCheckRedirect,
              redirect: '/apply/check/one',
              children: [
                {
                  path: 'one',
                  component: ApplyCheck,
                  meta: { requireLogin: true },
                  beforeEnter: (to, from, next) => {
                    // 客户信息已提交，在进入客户信息页面时跳过客户类型选择页面
                    if (store.state.applystore.cust_info_apply) {
                      next({ name: 'personal' })
                    } else {
                      next()
                    }
                  }
                },
                { path: 'personal', name: 'personal', component: ApplyPersonal, meta: { requireLogin: true } },
                { path: 'company', name: 'company', component: ApplyCompany, meta: { requireLogin: true } }
              ]
            },
            { path: 'assets', component: ApplyAssets, meta: { requireLogin: true } },
            { path: 'scheme', component: ApplyScheme, meta: { requireLogin: true } },
            { path: 'guarantee', component: ApplyGuarantee, meta: { requireLogin: true } },
            { path: 'attachment', component: ApplyAttachment, meta: { requireLogin: true } }
          ]
        },
        { path: 'query', component: Query },
        { path: 'create', component: Create },
        { path: 'report', component: Report, meta: { requireLogin: true } }
      ]
    },
    { path: '/login', component: Login, meta: { requireLogin: false } },
    { path: '/404', component: P404 },
    { path: '*', redirect: { path: '/404' } }
  ]
})

router.beforeEach((to, from, next) => {
  console.log(to.path)
  next()
})

export default router
